package com.china08.yxyapi.service.db.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.china08.yxyapi.entity.mg.attendance.YxyTeaAttendanceStatistics;
import com.china08.yxyapi.service.db.YxyTeaAttendanceStatisticsService;

@Repository
public class YxyTeaAttendanceStatisticsServiceImpl implements YxyTeaAttendanceStatisticsService {

    @Autowired
    MongoOperations mongoOperations;

    @Override
    public Map<String, Object> findList(String schoolId, String times, String status, Date month, String keyWord, Integer pageNum, Integer pageSize) {

        Integer skip = (pageNum - 1) * pageSize;

        Criteria criteria = new Criteria();
        criteria.and("schoolId").is(schoolId);
        if (StringUtils.isNotBlank(status)) {
            // 正常打卡
            if (status.equals("1")) {
                criteria.and("signStatus1").is(Integer.parseInt(status));
                criteria.and("signStatus2").is(Integer.parseInt(status));
                if (times.equals("4")) {
                    criteria.and("signStatus3").is(Integer.parseInt(status));
                    criteria.and("signStatus4").is(Integer.parseInt(status));
                }
            }
            if (status.equals("-1")) {
                if (times.equals("4")) {
                    criteria.orOperator(Criteria.where("signStatus1").ne(1), //
                            Criteria.where("signStatus2").ne(1), //
                            Criteria.where("signStatus3").ne(1), //
                            Criteria.where("signStatus4").ne(1));

                } else {
                    criteria.orOperator(Criteria.where("signStatus1").ne(1), Criteria.where("signStatus2").ne(1));
                }
            }
        }
        if (month != null) {
            String dayOfDate = new SimpleDateFormat("yyyy-MM").format(month);
            dayOfDate = dayOfDate.replace("-", "");
            Integer ltday = Integer.parseInt(dayOfDate + "31");
            Integer gtday = Integer.parseInt(dayOfDate + "00");
            criteria.and("dayOfDate").lte(ltday).gte(gtday);
        }

        if (StringUtils.isNotBlank(keyWord)) {
            criteria.and("teacherName").regex(".*?" + keyWord + ".*");
        }

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayOfDate")));
        // 总条数
        long count = mongoOperations.count(query, YxyTeaAttendanceStatistics.class);

        // 分页
        query.skip(skip);
        query.limit(pageSize);
        List<YxyTeaAttendanceStatistics> list = mongoOperations.find(query, YxyTeaAttendanceStatistics.class);

        // 返回
        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("count", count);
        returnMap.put("list", list);
        return returnMap;
    }

    @Override
    public List<YxyTeaAttendanceStatistics> findList(String schoolId, String times, String status, Date month, String keyWord) {
        Criteria criteria = new Criteria();
        criteria.and("schoolId").is(schoolId);

        if (StringUtils.isNotBlank(status)) {
            // 正常打卡
            if (status.equals("1")) {
                criteria.and("signStatus1").is(Integer.parseInt(status));
                criteria.and("signStatus2").is(Integer.parseInt(status));
                if (times.equals("4")) {
                    criteria.and("signStatus3").is(Integer.parseInt(status));
                    criteria.and("signStatus4").is(Integer.parseInt(status));
                }
            }
            if (status.equals("-1")) {
                if (times.equals("4")) {
                    criteria.orOperator(Criteria.where("signStatus1").ne(1), //
                            Criteria.where("signStatus2").ne(1), //
                            Criteria.where("signStatus3").ne(1), //
                            Criteria.where("signStatus4").ne(1));

                } else {
                    criteria.orOperator(Criteria.where("signStatus1").ne(1), Criteria.where("signStatus2").ne(1));
                }
            }
        }
        if (month != null) {
            String dayOfDate = new SimpleDateFormat("yyyy-MM").format(month);
            dayOfDate = dayOfDate.replace("-", "");
            Integer ltday = Integer.parseInt(dayOfDate + "31");
            Integer gtday = Integer.parseInt(dayOfDate + "00");
            criteria.and("dayOfDate").lte(ltday).gte(gtday);
        }
        if (StringUtils.isNotBlank(keyWord)) {
            criteria.and("teacherName").regex(".*?" + keyWord + ".*");
        }

        Query query = new Query();
        query.addCriteria(criteria);
        query.with(new Sort(new Order(Direction.DESC, "dayOfDate")));

        return mongoOperations.find(query, YxyTeaAttendanceStatistics.class);
    }
}
